package yxy.game.pm2.active.command.vip;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.vipdata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_VIP;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActVipBuyItemRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

/**
 * 购买vip特权商品
 * 
 * @author Dell
 *
 */
@MessageType(proto = Command.ActVipBuyItem, classtype = ActiveMessage.ActVipBuyItemRequest.class)
public class CMD_Active_VIP_BuyVipGoods extends Command<ActVipBuyItemRequest> {

	@Override
	public void execute(MessageSource messageSource, ActVipBuyItemRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActVipBuyItemResponse.Builder response = ActiveMessage.ActVipBuyItemResponse.newBuilder();
		int viplvgoods = request.getViplvgoods();

		PlayerBean playerBean = PlayerBean.get(pcode);
		int viplv = playerBean.getViplevel();
		if (viplvgoods > viplv) {
			send(messageSource, response, Result.NoViplv);
			return;
		}
		
		ActiveModule_VIP am = ActiveModule_VIP.get(pcode,false);

		ArrayList<Integer> list = am.hadbuyvipgoods();
		for (int i = 0; i < list.size(); i++) {
			int vlv = list.get(i);
			if (vlv == viplvgoods) {
				send(messageSource, response, Result.PayHadGetGoods);
				return;
			}
		}

		vipdata vd = DataCenter.getData(viplvgoods, vipdata.class);
		int cost = vd.getSpend();
		Currency cur = Currency.create(CurrencyType.CT_元宝, -1, cost);

		try {
			new CurrencyHandle().cost(pcode, CCReason.活动_VIP, cur);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}

		am.handlebuyvipgoods(viplvgoods);
		ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(vd.getCommoditytype(), vd.getCommodityid(), vd.getCommoditynum());
		new CurrencyHandle().addCurrency(pcode, CCReason.活动_VIP, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActVipBuyItemResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
